var AjaxComment = {};
AjaxComment.form = $("#comment-form");
AjaxComment.textbox = $("#textarea");
AjaxComment.submitButton = $("#submit");
AjaxComment.action = $("#comment-form").attr("action") + "?ajaxComments=yes";
AjaxComment.post = function() {
    AjaxComment.onBeforeComment();
    alert(AjaxComment.form.serialize());
    $.post(AjaxComment.action, AjaxComment.form.serialize(), AjaxComment.checkCommentResult, "text").fail(function() {
        AjaxComment.onCommentFailed("Connection Error")
    }).always(AjaxComment.onAfterComment)
};

// 评论成功显示
AjaxComment.onCommentSucceed = function(b) {
    if (b == "approved") {
        newBootstrapModal().setTitle("评论已发布").setContent("你的评论已经发布成功！").addButton("确定", "primary").show();
        AjaxComment.textbox.val("")
    } else {
        if (b == "waiting") {
            newBootstrapModal().setTitle("评论待审核").setContent("你的评论已经发布，但需要管理员进行审核").addContent("审核过程中你的评论会暂时隐藏").addButton("确定", "primary").show();
            AjaxComment.textbox.val("")
        } else {
            newBootstrapModal().setTitle("评论已发布").setContent("你的评论已经发布").addButton("确定", "primary").show();
            AjaxComment.textbox.val("")
        }
    }
};

// 评论失败显示
AjaxComment.onCommentFailed = function(b) {
    newBootstrapModal().setTitle("评论失败").setContent("你的评论发布失败").addContent("可能的原因: " + b).addButton("确定", "primary").show()
};

AjaxComment.checkCommentResult = function(g) {
    try {
        var h = $.parseJSON(g);
        return AjaxComment.onCommentSucceed(h.commentStatus)
    } catch (e) {
    }
    AjaxComment.onCommentFailed(g);
    g = g.replace(/[\r\n\t]/g, "").replace(/ {2,}/g, "");
    if (g.indexOf("<title>Error</title>") > 0) {
        var f = g.match(/<div class="container">(.+)<\/div>/);
        if (f != null) {
            return AjaxComment.onCommentFailed(f[1])
        }
    }
    AjaxComment.onCommentFailed("Unknown Error")
};

// 提交按钮事件禁用
AjaxComment.onBeforeComment = function() {
    $(AjaxComment.submitButton).attr("disabled", "disabled")
};

// 移除提交按钮事件禁用
AjaxComment.onAfterComment = function() {
    $(AjaxComment.submitButton).removeAttr("disabled")
};

$(document).ready(function() {
    $(AjaxComment.submitButton).click(function(b) {
        AjaxComment.post();
        b.preventDefault();
        return false
    })
});

function newBootstrapModal() 
{
    var k = {};
    k.beforeClose = function() { };
    k.beforeShow = function() { };
    
    k.setTitle = function(a) 
    {
        o.innerHTML = "<p>" + a + "</p>";
        return this
    };
    
    k.setContent = function(a) 
    {
        p.innerHTML = "<p>" + a + "</p>";
        return this
    };
    
    k.addContent = function(a) 
    {
        p.innerHTML += "<p>" + a + "</p>";
        return this
    };
    
    k.hide = function(a) 
    {
        k.beforeClose();
        $(j).modal("hide");
        return this
    };
    
    k.show = function() 
    {
        document.body.appendChild(j);
        $(j).on("hidden.bs.modal", function() {
            $(j).remove()
        });
        k.beforeShow();
        $(j).modal("show");
        return this
    };
    
    k.addButton = function(b, d, a) 
    {
        var c = document.createElement("button");
        c.className = d == null ? "btn btn-flat" : "btn btn-flat btn-" + d;
        c.innerHTML = b;
        c.onclick = function() {
            if (typeof a == "function") {
                a()
            }
            k.hide()
        };
        q.appendChild(c);
        return this
    };
    
    var j = document.createElement("div");
    j.className = "modal fade";
    j.tabIndex = "-1";
    var m = document.createElement("div");
    m.className = "modal-dialog";
    var l = document.createElement("div");
    l.className = "modal-content";
    var n = document.createElement("div");
    n.className = "modal-header";
    var r = document.createElement("button");
    r.type = "button";
    r.className = "close";
    r.setAttribute("aria-hidden", "true");
    r.innerHTML = "&times;";
    r.onclick = k.hide;
    var o = document.createElement("h4");
    o.className = "modal-title";
    n.appendChild(r);
    n.appendChild(o);
    var p = document.createElement("div");
    p.className = "modal-body";
    var q = document.createElement("div");
    q.className = "modal-footer";
    l.appendChild(n);
    l.appendChild(p);
    l.appendChild(q);
    m.appendChild(l);
    j.appendChild(m);
    return k
};
